//
//  CCPopViewController.swift
//  KastKing
//
//  Created by liujianlong on 2022/11/15.
//

import UIKit
import SnapKit
#if canImport(TCC_String)
import TCC_String
#endif
#if canImport(TCC_Extension)
import TCC_Extension
#endif
#if canImport(TCC_View)
import TCC_View
#endif
#if canImport(TCC_ViewController)
import TCC_ViewController
#endif
/// 下拉pop小弹窗
@objc public protocol CCPopViewControllerDelegate : UIPopoverPresentationControllerDelegate
{
    /// 选中的cell
    @objc func select(_ index : Int , _ title : String)
}
@objc open class CCPopViewController: UIViewController {
    open var tableView: UITableView!
    var delegate : CCPopViewControllerDelegate?
    open var popover : UIPopoverPresentationController?
    open override func viewDidLoad() {
        super.viewDidLoad()
        self.initUI()
    }
    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.view.bgHexColor = "#FFFFFF"
    }
    open func initUI() {
        self.tableView = UITableView(frame: .zero, style: .plain)
        self.tableView.bgHexColor = "#FFFFFF"
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.separatorStyle = .none
        self.view.addSubview(self.tableView)
        self.tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        self.popover?.backgroundColor = .white
        _ = self.view.shadow()
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(actionForTap))
        tap.cancelsTouchesInView = false
        self.view.addGestureRecognizer(tap)
        
    }
    open var array = [String](){
        didSet{
            self.tableView.reloadData()
        }
    }
    
    @objc private func actionForTap(){
        self.view.endEditing(true)
    }
    
    open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard self.firstResponder(self.view) != nil else{
            let touch : UITouch = (touches as NSSet).allObjects.last as! UITouch
            if touch.view?.isDescendant(of: self.view.subviews.first ?? UIView()) == false{
                self.dismiss(animated: false)
            }
            return }
        super.touchesEnded(touches, with: event)
        self.view.endEditing(true)
    }
}

//MARK: - UITableViewDataSource
extension CCPopViewController : UITableViewDataSource
{
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.array.count
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        var cell = tableView.dequeueReusableCell(withIdentifier: "popcell")
        if cell == nil
        {
            cell = UITableViewCell.init(style: .default, reuseIdentifier: "popcell")
        }
        cell!.contentView.removeAllSubView()
        
        let label = UILabel()
        label.text = self.array[indexPath.row]
        label.font = UIFont.systemFont(ofSize: 14, weight: .semibold)
        label.textColor = "#000000".color()
        label.textAlignment = .center
        cell!.contentView.addSubview(label)
        
        label.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        let line = UIView()
        line.backgroundColor = "#EEEEEE".color()
        cell?.contentView.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(16)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        cell?.bgHexColor = "#FFFFFF"
        cell?.selectionStyle = .none
        return cell!
    }
}
//MARK: - UITableViewDelegate
extension CCPopViewController : UITableViewDelegate
{
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        self.dismiss(animated:true, completion:nil)
        self.delegate?.select(indexPath.row, self.array[indexPath.row])
    }
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
        return 50
    }
}
